package cn.lopr.hfbdqn.service;

import cn.lopr.hfbdqn.annotaion.Auth;
import cn.lopr.hfbdqn.common.Result;
import cn.lopr.hfbdqn.configuration.AuthInfo;
import cn.lopr.hfbdqn.dao.CommentMapper;
import cn.lopr.hfbdqn.pojo.Comment;
import cn.lopr.hfbdqn.pojo.User;
import cn.lopr.hfbdqn.route.CommentRoute;
import cn.lopr.hfbdqn.vo.CommentVo;
import cn.lopr.mvc.frame.annotaion.Autowired;
import cn.lopr.mvc.frame.annotaion.Service;
import cn.lopr.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * @author shuangyang7
 * @date 2021/7/19 11:03
 */
@Service
public class CommentService implements CommentRoute {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public Result<?> getByMovieId(Integer movieId) {
        List<Comment> comments = commentMapper.selectByMovieId(movieId);
        return new Result<>(Result.SUCCESS, comments);
    }

    @Override
    public Result<?> getCommentByMovieId(Integer movieId, boolean isAll) {
        List<CommentVo> comments;
        if (isAll) {
            comments = commentMapper.selectCommentByMovieId(movieId);
        } else {
            comments = commentMapper.selectByMovieIdLimit6(movieId);
        }

        return new Result<>(Result.SUCCESS, comments);
    }

    @Auth
    @Override
    public Result<?> addComment(Comment comment) {
        User user = AuthInfo.get();
        comment.setUserId(user.getUserId());
        comment.setCommentDate(new Date());
        if (comment.getCommentContent().length() > 250) {
            comment.setCommentContent(comment.getCommentContent().substring(0, 250));
        }
        int i = commentMapper.insertSelective(comment);
        return new Result<>(Result.SUCCESS, comment);
    }

    @Auth
    @Override
    public Result<?> getCommentById(Integer id) {
        User user = AuthInfo.get();
        Comment comment = commentMapper.selectByPrimaryKey(id);
        return new Result<>(Result.SUCCESS, comment);
    }

    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("DateUtils.formatYMDHMS(date) = " + DateUtils.formatYMDHMS(date));
    }

}
